#include "o3_photometer_content.h"

O3_Photometer_Content::O3_Photometer_Content(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Combox_Init();
    ui_Btn_Init();
   ui_Label_Init();
   connect_Init();
}


void O3_Photometer_Content::connect_Init()
{
    //第一个选择下拉菜单
    connect(theComboBox,&MyComboBox::clicked,[=](){
        {
//              theComboBox->view()->selectionModel()->setCurrentIndex(theComboBox->model()->index(index,theComboBox->modelColumn(),theComboBox->rootModelIndex()),QItemSelectionModel::ClearAndSelect);
            theComboBox->showPopup();
            theComboBox->view()->setFocus();
            flag=1;
            qDebug()<<"2";
        }

    });

    connect(theComboBox,&QComboBox::currentTextChanged,[=](){
        {
            theLabel[1]->setText(theComboBox->currentText());
        }
    });
    connect(theComboBox2,&QComboBox::currentTextChanged,[=](){
        {

            theLabel[3]->setText(theComboBox2->currentText());
        }
    });

    //第二个选择下拉菜单
    connect(theComboBox2,&MyComboBox::clicked,[=](){
        {
            theComboBox2->showPopup();
            theComboBox2->view()->setFocus();
            flag=1;
        }

    });

    connect(theComboBox3,&QComboBox::currentTextChanged,[=](){
        {

            theLabel[3]->setText(theComboBox3->currentText());
        }
    });

    //第二个选择下拉菜单
    connect(theComboBox3,&MyComboBox::clicked,[=](){
        {
            theComboBox3->showPopup();
            theComboBox3->view()->setFocus();
            flag=1;
        }

    });

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
//        temp_Gas=(theComboBox->currentIndex() - 1);
//        temp_Gas_select=(theComboBox2->currentIndex() - 1);
//        theLabel[7]->setText(QString::number(Sysset->Gas[temp_Gas].gasconcentration_span[temp_Gas_select],'f',3));
//        btnlist_ui.at(2)->setText(QString("%1").arg(Sysset->Gas[temp_Gas].totalflow_span[temp_Gas_select]));

        if(theComboBox->currentIndex() == theComboBox->count()-1)
        {
            for(int i=4;i<Label_Num-3;i++)
                theLabel[i]->show(); //当点击确定时显示隐藏的label
            theLabel[Label_Num-3]->hide(); //当点击确定时显示隐藏的label
            theLabel[Label_Num-2]->hide(); //当点击确定时显示隐藏的label
            theLabel[Label_Num-1]->hide(); //当点击确定时显示隐藏的label
            Sysset->IS_OZO_ENABLE=1;
            Sysctrl->Sys_Gas_Sel = 1;
            Sysctrl->Sys_Span_Sel=8;
            Sysctrl->Sys_Ozo_Sel = Sysset->Sys_Ozo_Sel = 7;
            Sysctrl->Total_Flow = Sysset->Ozo_ZeroAir;
            Sysctrl->Target_Consentration=0;
            Sysctrl->BD_Ozonator.Vot_Set = Sysset->Ozo_Span[6]*0.132+5.7;
            Sysctrl->Para_Save =1;
            O3_alone=1;

        }
        else
        {
            for(int i=4;i<Label_Num;i++)
            theLabel[i]->show(); //当点击确定时显示隐藏的label
            qDebug()<<theComboBox->currentIndex();

            if(O3_Nong==0)
            {
                Sysctrl->Sys_Ozo_Sel =Sysset->Sys_Ozo_Sel= Sysset->Sys_Ozo_Sel = O3_Span;
                Sysctrl->Sys_Gas_Sel=Sysset->Sys_Gas_Sel=Sysset->Sys_Gas_Sel = ABC_choose;
                Sysctrl->Sys_Span_Sel=Sysset->Sys_Span_sel =Sysset->Sys_Span_sel= 8;
                Sysctrl->Total_Flow= Sysset->Gas[ABC_choose-1].zeroairflow;
                Sysctrl->Target_Consentration = 0;
            }
            else
            {
                Sysctrl->Sys_Ozo_Sel =Sysset->Sys_Ozo_Sel =Sysset->Sys_Ozo_Sel = O3_Span;
                Sysctrl->Sys_Gas_Sel=Sysset->Sys_Gas_Sel=Sysset->Sys_Gas_Sel =ABC_choose;
                Sysctrl->Sys_Span_Sel=Sysset->Sys_Span_sel= Sysset->Sys_Span_sel= O3_Nong;
                Sysctrl->Total_Flow= Sysset->Gas[ABC_choose-1].totalflow_span[O3_Nong-1];
                Sysctrl->Target_Consentration = Sysset->Gas[ABC_choose-1].gasconcentration_span[O3_Nong-1];
            }
            Sysctrl->BD_Ozonator.Vot_Set = Sysset->Ozo_Span[O3_Span-1]*0.132+5.7;
            Sysset->IS_OZO_ENABLE = 1;
            Sysset->Sys_Only_Ozo = O3_alone=0;
            Sysctrl->Para_Save = 1;
        }
    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });


    //修改手动时的数据
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){

            keyPad *board = new keyPad(this);
             board->show();
            connect(board,&keyPad::changename2,this,&O3_Photometer_Content::Recive_Gas_SelData);
            connect(board,&keyPad::destroyed,[=](){
                btnlist_ui.at(2)->setFocus();
            });
    });
}

void O3_Photometer_Content::ui_renew_Init()
{
    if(theComboBox->currentIndex() == theComboBox->count()-1)
    {
        btnlist_ui.at(2)->setText(QString::number(Sysset->Ozo_Span[6],'f',1));
        btnlist_ui.at(2)->setEnabled(true);
        theLabel[6]->setText(tr("当前值："));
        theLabel[8]->setText(tr("%"));
        theLabel[9]->setText(tr("气体浓度："));
        btnlist_ui.at(2)->setText(QString::number(Sysset->Ozo_Span[6],'f',1));
        theLabel[11]->setText(tr("SCCM"));
    }
    else
    {

        //显示数据
        theLabel[7]->setNum(Sysset->Ozo_Span[O3_Span-1]);
        if(O3_Nong==0)  //零气
        {
            btnlist_ui.at(2)->setText(0);
            theLabel[13]->setText(QString::number(Sysset->Gas[ABC_choose-1].zeroairflow,'f',0));
        }
        else  //标气
        {
            btnlist_ui.at(2)->setText(QString::number(Sysset->Gas[ABC_choose-1].gasconcentration_span[O3_Nong-1],'f',0));
            theLabel[13]->setText(QString::number(Sysset->Gas[ABC_choose-1].totalflow_span[O3_Nong-1],'f',0));
        }

        theLabel[9]->setText(tr("气体浓度："));
        btnlist_ui.at(2)->setText(0); //气体浓度
        theLabel[11]->setText(tr("SCCM"));
        theLabel[12]->setText(tr("零气流量："));
        theLabel[13]->setText(QString("%1").arg(Sysset->Gas[ABC_sel-1].zeroairflow,'f'));//零气流量
        theLabel[14]->setText(tr("SCCM"));


    }
}

void O3_Photometer_Content::Recive_Gas_SelData(QString s)
{
    if(s.toFloat()>=0 && s.toFloat()<=100)
    {
        Sysset->Ozo_Span[6] = s.toFloat();
    }
    else if(s.toFloat() <0)
    {
        Sysset->Ozo_Span[6] = 0;
    }
    else if(s.toFloat() >100)
    {
        Sysset->Ozo_Span[6] = 100;
    }
    btnlist_ui.at(2)->setText(QString::number(Sysset->Ozo_Span[6],'f',1));
}

bool O3_Photometer_Content::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
//          for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched||theComboBox == watched||theComboBox->view() == watched||theComboBox2 == watched||theComboBox2->view() == watched||theComboBox3 == watched||theComboBox3->view() == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
                //qDebug()<<focusWidget()->objectName();
                QKeyEvent *ev = (QKeyEvent *)event;
                QPushButton *button = (QPushButton *)watched;
                MyComboBox *boBox = (MyComboBox *)watched;

                if(flag==1)//当下拉显示的时候
               {
                   switch (ev->key()) {
                   case UP:
                   {
                       //qDebug()<<"I";
                       if(focusWidget()== theComboBox) boBox = theComboBox;
                       if(focusWidget()== theComboBox2) boBox = theComboBox2;
                       if(focusWidget()== theComboBox3) boBox = theComboBox3;
                       if(boBox->currentIndex()>0)
                           boBox->setCurrentIndex(boBox->currentIndex() - 1);
                       else
                           boBox->setCurrentIndex(boBox->count()-1);
                           boBox->showPopup();
                       break;
                   }
                   case DOWN:
                   {
                       //qDebug()<<"K";

                       if(focusWidget()== theComboBox) boBox = theComboBox;
                       if(focusWidget()== theComboBox2) boBox = theComboBox2;
                       if(focusWidget()== theComboBox3) boBox = theComboBox3;
                           if(boBox->currentIndex() < boBox->count()-1)
                               boBox->setCurrentIndex(boBox->currentIndex()+1);
                           else
                               boBox->setCurrentIndex(0);
                           boBox->showPopup();

                       break;
                   }
                   case OK:
                   {
                       if((flag==1))
                       {
                           theComboBox->hidePopup();
                           theComboBox2->hidePopup();
                           theComboBox3->hidePopup();
                           flag=2;
                       }
                       else
                       {
                           emit  button-> clicked();
                           emit boBox-> clicked();
                       }
                        break;
                   }
                   default:
                       break;
                   }
               }
               else
               {
                   switch (ev->key())
                   {
                   case OK:
                   {

                       emit  button-> clicked();
                       emit  boBox-> clicked();
                     break;
                   }
                   case UP: //Up
                     {
//                         if(focusWidget() != btnlist_ui.first())
                            focusPreviousChild();
                       break;
                     }
                   case DOWN: //Down
                     {
//                         if(focusWidget() != btnlist_ui.back())
                            focusNextChild();
                       break;
                     }
                   case BACK:
                   {
                       this->parentWidget()->show();
                       this->deleteLater();
                       break;
                   }
                   case INIT:
                   {

                       init_Show *show = new init_Show(this);
                       show->show();
                       this->hide();
                       break;
                   }
                    default:
                       break;
                   }
               }
            }
            }
}

void O3_Photometer_Content::ui_Label_Init()
{

    for(int i=0;i<Label_Num;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
    theLabel[0]->setGeometry(50,120,200,40);//设定每隔放置一个按键
    theLabel[1]->setGeometry(300,120,100,40);//设定每隔放置一个按键
    theLabel[2]->setGeometry(50,160,200,40);//设定每隔放置一个按键
    theLabel[3]->setGeometry(300,160,100,40);//设定每隔放置一个按键
    theLabel[4]->setGeometry(50,200,200,40);//设定每隔放置一个按键
    theLabel[5]->setGeometry(300,200,100,40);//设定每隔放置一个按键
    theLabel[6]->setGeometry(50,220,200,40);//设定每隔放置一个按键
    theLabel[7]->setGeometry(300,220,100,40);//设定每隔放置一个按键
    theLabel[8]->setGeometry(420,220,100,40);//设定每隔放置一个按键
    theLabel[9]->setGeometry(50,270,200,40);//设定每隔放置一个按键
    btnlist_ui.at(2)->setGeometry(300,270,100,40);//设定每隔放置一个按键
    theLabel[11]->setGeometry(420,270,100,40);//设定每隔放置一个按键
    theLabel[12]->setGeometry(50,320,200,40);//设定每隔放置一个按键
    theLabel[13]->setGeometry(300,320,100,40);//设定每隔放置一个按键
    theLabel[14]->setGeometry(420,320,100,40);//设定每隔放置一个按键


    theLabel[0]->setText(tr("当前气体为："));
    theLabel[1]->setText(theComboBox->currentText());
    theLabel[2]->setText(tr("当前跨度为："));
    theLabel[3]->setText(theComboBox2->currentText());
    theLabel[4]->setText(tr("当前浓度为："));
    theLabel[5]->setText(theComboBox3->currentText());
    theLabel[6]->setText(tr("O3浓度："));
    theLabel[7]->setText(QString::number(Sysset->Gas[temp_Gas].gasconcentration_span[temp_Gas_select],'f',3));
    theLabel[8]->setText(tr("PPM"));
    theLabel[9]->setText(tr("气体浓度："));
    btnlist_ui.at(2)->setText(QString("%1").arg(Sysset->Gas[temp_Gas].totalflow_span[temp_Gas_select]));
    theLabel[11]->setText(tr("SCCM"));
    theLabel[12]->setText(tr("总流量："));
    theLabel[13]->setText(QString("%1").arg(Sysset->Gas[temp_Gas].totalflow_span[temp_Gas_select]));
    theLabel[14]->setText(tr("SCCM"));

    for(int i=4;i<Label_Num;i++)
    theLabel[i]->hide(); //当进入界面时隐藏部分label
}

void O3_Photometer_Content::ui_Combox_Init()
{
    {

        QStringList strList;
        QStringList strList2;
        QStringList strList3;
        strList<<"浓度1"<<"浓度2"<<"浓度3"<<"浓度4"<<"浓度5"<<"浓度6"<<"手动";
//        strList2<<"浓度1"<<"浓度2"<<"浓度3"<<"浓度4"<<"浓度5"<<"浓度6"<<"手动";

        for(int i=0;i<Sysset->Gas_Num;i++)
        strList2<<Sysset->Gas[i].gasname;

    strList3<<"零气"<<"标气1"<<"标气2"<<"标气3"<<"标气4"<<"标气5"<<"标气6"<<"自动";

    theComboBox = new MyComboBox(this);
    theComboBox->addItems(strList);
    theComboBox->setGeometry(30,50,150,60 );
    theComboBox->installEventFilter(this);
    theComboBox->view()->installEventFilter(this);

    theComboBox2 = new MyComboBox(this);
    theComboBox2->addItems(strList2);
    theComboBox2->setGeometry(230,50,150,60 );
    theComboBox2->installEventFilter(this);
    theComboBox2->view()->installEventFilter(this);

    theComboBox3 = new MyComboBox(this);
    theComboBox3->addItems(strList3);
    theComboBox3->setGeometry(430,50,150,60 );
    theComboBox3->installEventFilter(this);
    theComboBox3->view()->installEventFilter(this);
    theComboBox->setStyleSheet("QComboBox{color:white;      /* 字体颜色*/\
                               background-color: rgb(39, 170, 255);\
                               border-radius:4px;\
                               border: 1px groove white; \
                               padding:1px 30px;\
                                border-style: outset;}"
    "QComboBox:focus{color:rgb(250,250, 250);\
                                background-color: rgb(255, 170, 39);\
                                border-radius:4px;\
                                border: 2px rgb(110, 111, 215); \
                                padding:1px 30px;\
                                border-style: outset;}"
    );
theComboBox2->setStyleSheet(theComboBox->styleSheet());
theComboBox3->setStyleSheet(theComboBox->styleSheet());

    }

}

void O3_Photometer_Content::ui_Btn_Init()
{

    for(int i=0;i<3;i++)//创建主屏幕显示的按键
    {

         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
        theBtn[i]->setFixedSize(120,90);
        theBtn[i]->geometry();
        theBtn[i]->setStyleSheet("QPushButton{\
                               outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                               background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                               border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                               border-style: outset;\
                               /* 按钮背景图标设置 */\
                               background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                               background-origin: content;\
                               background-position: top;	         /* 背景图片的位置 */\
                               padding-right: 0px;	    /*内边距设置*/	    \
                               padding-left: 0px;		/*  */	\
                               padding-bottom: 10px;		/*  */    \
                               padding-top: 10px;		/*  */    \
                               background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                               /* 按钮文本设置 */\
                               text-align: bottom;			             /* 文本的位置 */\
                               font-size: 20px;\
                               color: #FFFFFF;			                 /* 文本颜色 */\
                          }"
    "QPushButton:focus{\
outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
    background-color: rgb(255,125,25);	    /* 背景颜色 */\
    border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
    border-style: outset;\
    /* 按钮背景图标设置 */\
    background-image: url(:/pic/access/过滤.png); /* 背景图片 */\
    background-origin: content;\
    background-position: top;	         /* 背景图片的位置 */\
    padding-right: 0px;	    /*内边距设置*/	    \
    padding-left: 0px;		/*  */	\
    padding-bottom: 10px;	/*  */   \
    padding-top: 10px;		/*  */    \
    background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
    /* 按钮文本设置 */\
    text-align: bottom;			             /* 文本的位置 */\
    font-size: 22px;\
    qproperty-iconSize: 20px 20px;			/* 设置图标大小*/\
color: rgb(125, 255, 250);			                 /* 文本颜色 */\
}"
"QPushButton:pressed{background-color:rgb(99,211,133);}"
"QPushButton:checked{background-color:rgb(66,66,99);}"
//"QPushButton:hover{background-color:rgb(255,111,111);}"
//"QPushButton:MouseButtonPress{background-color:rgb(255,111,111);}"
    );
//        newbutton->setIconSize(QSize(1,2));

}
btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);
btnlist_ui.append(theBtn[2]);

btnlist_ui.at(0)->setGeometry(150,360,120,90 );
btnlist_ui.at(1)->setGeometry(360,360,120,90 );
btnlist_ui.at(2)->setGeometry(300,280,100,40 );
btnlist_ui.at(0)->setText(tr("确定"));
btnlist_ui.at(1)->setText(tr("返回"));
btnlist_ui.at(2)->setEnabled(true);
btnlist_ui.at(2)->hide();
//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}

